//
//  WebViewController.m
//  ChildHoodStemp
//
//  Created by steven_l on 15/4/18.
//
//

#import "WebViewController.h"

#import "UIUtil.h"
#import "MicroDef.h"
#import "AppDelegate.h"


static float kBottomBarHeight = 44;
@interface WebViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UIButton *forwardBtn;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitle:@"链接"];
//    [self.navigationItem setLeftItemWithTarget:self action:@selector(back) image:@"back_bar_button.png" selectedImageName:@"back_bar_button_tapped.png" text:nil disabled:nil];
    [self.view setBackgroundColor:CommonColor];
//    NSLog(@"%@",[self.navigationController class]);
    [self.navigationController.navigationBar setHidden:NO];
    [self creatWeiWebViewWithUrl:_webUrl];
//    NSLog(@"%@ -- %d",self.navigationController.navigationBar,self.navigationController.navigationBar.hidden);/
    // Do any additional setup after loading the view.
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;

}

- (void)back
{
    self.navigationController.navigationBarHidden = YES;
//    CHSNavigationController *nav =  (CHSNavigationController *)[AppDelegate shareDelegate].window.rootViewController;
//    nav.navigationBar.hidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatWeiWebViewWithUrl:(NSURL *)url
{
    // 车辆列表
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT -44-20-kBottomBarHeight)];
    _webView.delegate = self;
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:_webView];
    
    [self initBottomBar];
}
- (void)initBottomBar
{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_webView.frame), HARDWARE_SCREEN_WIDTH, kBottomBarHeight)];
    bottomView.backgroundColor = RGBCOLOR(230, 231, 234);
    [self.view addSubview:bottomView];
    
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn setImage:[UIImage imageNamed:@"WebView_Backward_disable.png"] forState:UIControlStateDisabled];
    [_backBtn setImage:[UIImage imageNamed:@"WebView_Backward.png"] forState:UIControlStateNormal];
    [_backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    _backBtn.frame = CGRectMake(30, 5, 34, 34);
    [bottomView addSubview:_backBtn];
    
    self.forwardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_forwardBtn setImage:[UIImage imageNamed:@"WebView_Forward_disable.png"] forState:UIControlStateDisabled];
    [_forwardBtn setImage:[UIImage imageNamed:@"WebView_Forward.png"] forState:UIControlStateNormal];
    _forwardBtn.frame = CGRectMake(CGRectGetMaxX(_backBtn.frame)+30, 5, 34, 34);
    [_forwardBtn addTarget:self action:@selector(goForward) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:_forwardBtn];
    
    [self decideBottomBtnsState];
    UIButton *refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [refreshBtn setImage:[UIImage imageNamed:@"WebView_Refresh.png"] forState:UIControlStateNormal];
    refreshBtn.frame = CGRectMake(HARDWARE_SCREEN_WIDTH-30-34, 5, 34, 34);
    [refreshBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:refreshBtn];
}

#pragma mark - 按钮状态
- (void)goBack
{
    [_webView goBack];
}

- (void)goForward
{
    [_webView goForward];
}

- (void)refresh
{
    [_webView reload];
}


//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//    return YES;
//}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self decideBottomBtnsState];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self decideBottomBtnsState];
}


- (void)decideBottomBtnsState
{
    if (_webView.canGoBack) {
        [_backBtn setEnabled:YES];
    }
    else
    {
        [_backBtn setEnabled:NO];
    }
    
    if(_webView.canGoForward)
    {
        [_forwardBtn setEnabled:YES];
    }
    else
    {
        [_forwardBtn setEnabled:NO];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
